<template>
  <div class="container">
    <web-view :src="link+'?type=miniprogram'+'&user_id='+user_id+'&user_token='+user_token+'&code='+code+'&share_id='+share_id"
              @message="h5PostMessage"></web-view>
  </div>
</template>

<script>
import common from '@/assets/js/mmk_common.js'
export default {
  data () {
    return {
      link: "",
      user_token: "",
      user_id: "",
      h5Data: {},
      code: 0,// 清除缓存随机数
      share_id: 0
    };
  },

  methods: {
    //h5向小程序发送的数据
    h5PostMessage (e) {
      //当用户点击这个h5页面的分享时获取到
      this.h5Data = e.mp.detail.data[0];
    },
  },
  onLoad (options) {
    let that = this
    if (decodeURIComponent(options.url)) {
      this.link = decodeURIComponent(options.url)

    } else {
      this.link = options.url;
    }
    console.log(this.link);
    this.code = new Date().getTime()
    if (options.share_id) {
      console.log(options.share_id);
      this.share_id = options.share_id
    }
    wx.getStorage({
      key: "user_token",
      success: res => {
        that.user_token = res.data;
      },
      fail: () => { },
      complete: () => { }
    });
    wx.getStorage({
      key: 'user_id',
      success: (res) => {
        that.user_id = res.data
      },
      fail: () => { },
      complete: () => { }
    })
  },
  onShow () {
    let that = this;
    wx.getStorage({
      key: "user_token",
      success: res => {
        that.user_token = res.data;
      },
      fail: () => { },
      complete: () => { }
    });
    wx.getStorage({
      key: 'user_id',
      success: (res) => {
        that.user_id = res.data
      },
      fail: () => { },
      complete: () => { }
    })
  },
  watch: {
    h5Data (newVal) {
      this.h5Data = newVal
    },
    link (newVal) {
      this.link = newVal
    },
    user_token (newVal) {
      this.$forceUpdate()
    },

  },
  onShareAppMessage (options) {
    if (options.webViewUrl.includes('https://h5s.ameimeika.com/lucky/index.html')) {
      common.fly_post('api/v4_2/activity/add/count', {}, result => {
      })
    }
    return {
      title: this.h5Data ? this.h5Data.title : "美美咖+",
      path: this.h5Data ? `${this.h5Data.path}&sid=${this.user_id || ''}` : `pages/package_main/advertisement/main?url=${options.webViewUrl}`,
      imageUrl: this.h5Data ? this.h5Data.imageUrl : "",
      success: res => {
        wx.showToast({
          title: "分享成功", //提示的内容,
          icon: "none", //图标,
          duration: 2000, //延迟时间,
          mask: false //显示透明蒙层，防止触摸穿透,
        });
      },
      fail: () => {
        wx.showToast({
          title: "分享未成功", //提示的内容,
          icon: "none", //图标,
          duration: 2000, //延迟时间,
          mask: false //显示透明蒙层，防止触摸穿透,
        });
      },
      complete: () => { }
    };
  }
};
</script>

<style lang="less" scoped>
</style>

